      package inet.util;
      
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.PrintStream;
import java.util.Date;
import java.util.Vector;
      
      public class FileTool
      {
        public static Vector getAllFiles(File location, String fileExt)
        {
        Vector v = new Vector();
        File[] list = location.listFiles();
        for (int i = 0; i < list.length; ++i) {
          if (list[i].toString().endsWith(fileExt)) {
            v.addElement(list[i]);
            }
      
          }
      
        return v;
        }
      
        public static void copy(String source_name, String dest_name) throws IOException
        {
        File source_file = new File(source_name);
        File dest_file = new File(dest_name);
        copy(source_file, dest_file);
        }
      
        public static void copy(File source_file, File dest_file) throws IOException {
        FileInputStream source = null;
        FileOutputStream destination = null;
      
        if ((!(source_file.exists())) || (!(source_file.isFile())))
          throw new FileCopyException("FileCopy: no such source file: " + 
            source_file.getPath());
        if (!(source_file.canRead())) {
          throw new FileCopyException("FileCopy: source file is unreadable: " + 
            source_file.getPath());
          }
      
        source = new FileInputStream(source_file);
        destination = new FileOutputStream(dest_file);
        byte[] buffer = new byte[1024];
          int bytes_read;
        while ((bytes_read = source.read(buffer)) != -1)
          {
          //int bytes_read_;
          destination.write(buffer, 0, bytes_read);
          }
          try
          {
          if (source != null)
            source.close();
          if (destination != null)
            destination.close();  } catch (IOException localIOException) {
          }
        }
      
        public static void move(String source_name, String dest_name) throws IOException {
        File source_file = new File(source_name);
        File dest_file = new File(dest_name);
      
        copy(source_file, dest_file);
        source_file.delete();
        }
      
        public static void move(File source_file, File dest_file) throws IOException {
        copy(source_file, dest_file);
        source_file.delete();
        }
      
        public static byte[] readFile(String filename) throws IOException {
        byte[] buffer = (byte[])null;
        FileInputStream fin = new FileInputStream(filename);
        buffer = new byte[fin.available()];
        fin.read(buffer);
        return buffer;
        }
      
        public static void saveToFile(String output, String filename) {
          try {
          String text=filename.substring(0,4); 
          Date now=new Date();
          
          if (((text.equals("fail"))||(text.equals("log_"))))  {
        	  //tao file theo ngay, h thang nam
        	  String url=createFolder(now, true);        	  
        	  File f=new File (url+"\\"+filename);
        	  FileOutputStream out = new FileOutputStream(f, true);      
              PrintStream ps=new PrintStream(out);
              ps.println(output);
              ps.close();  
          }          
          else {
        	  String url=createFolder(now, false);
        	  File f = new File(url+"\\"+filename);
              FileOutputStream out = new FileOutputStream(f, true);      
              PrintStream ps=new PrintStream(out);
              ps.println(output);
              ps.close();  
          }
          
          } catch (Exception e) {
          System.out.println(e.getMessage());
          }
        }
      
        public static void saveToSuccessFile(String output, String filename) {
            try {
            File f = new File("log\\success\\"+filename);
            FileOutputStream out = new FileOutputStream(f, true);      
            PrintStream ps=new PrintStream(out);
            ps.println(output);
            ps.close();
            } catch (Exception e) {
            System.out.println(e.getMessage());
            }
          }
        
        public static void main(String[] args)
        {
       FileTool fTool = new FileTool();
       String s = "newFolder";
       File f = new File(s);
       Vector v = getAllFiles(f, ".txt");
       System.out.println("Size: " + v.size());
        }
        
        public static String createFolder(Date now, boolean isError){
    		String url="";
    		if (!isError){
    			try {
    		          String year=String.valueOf(now.getYear()+1900);
    		       	  String month=String.valueOf(now.getMonth()+1);
    		       	  String date=String.valueOf(now.getDate());    		       
    		       	  File f1=new File ("log\\success\\"+year);
    		       	  f1.mkdir();
    		          File f2=new File ("log\\success\\"+year+"\\"+month);
    		       	  f2.mkdir();
    		       	  File f3=new File ("log\\success\\"+year+"\\"+month+"\\"+date);
    		          f3.mkdir();
    		          url="log\\success\\"+year+"\\"+month+"\\"+date;
    		         }
    		        catch (Exception e){
    		        	e.printStackTrace();
    		        }
    		}
    		else {
    			try {
    		          String year=String.valueOf(now.getYear()+1900);
    		       	  String month=String.valueOf(now.getMonth()+1);
    		       	  String date=String.valueOf(now.getDate());		       	  
    		       	  File f1=new File ("log\\fail_error\\"+year);
    		       	  f1.mkdir();
    		          File f2=new File ("log\\fail_error\\"+year+"\\"+month);
    		       	  f2.mkdir();
    		       	  File f3=new File ("log\\fail_error\\"+year+"\\"+month+"\\"+date);
    		          f3.mkdir();
    		          url="log\\fail_error\\"+year+"\\"+month+"\\"+date;
    		       	  
    		         }
    		        catch (Exception e){
    		        	e.printStackTrace();
    		        }
    		}
    		return url;
    		
    	}
        
      }


 